package com.atguigu.algorithm;

public class ReplaceBlank {
    public static void main(String[] args) {
        System.out.println(replaceSpace("We are Happy"));
    }
    public static String replaceSpace(String s){
        int count=0;
        for(int i=0;i<s.length();i++){
            //判断有几个空格
            if(s.charAt(i)==' '){
                count++;
            }
        }
        char[] chars=new char[s.length()+2*count];
        for(int i=0,j=0;i<s.length();i++){
            //j是专门记录新数组的下标指针，i是旧数组的下标指针
            if(s.charAt(i)!=' '){
                chars[j++]=s.charAt(i);
            }else{
                chars[j++]='%';
                chars[j++]='2';
                chars[j++]='0';
            }
        }
        return String.valueOf(chars);
    }
}
